<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //1. then的链式简单实现；

      // p.then().then().then()

      class myPromise {
        constructor(handle) {
          this["status"] = "pedding";
          this["PromiseResult"] = undefined;
          handle(this._resolve.bind(this), this._reject.bind(this));
        }
        _resolve(arg) {
          this["status"] = "fufilled";
          this["PromiseResult"] = arg;
        }
        _reject(arg) {
          this["status"] = "rejected";
          this["PromiseResult"] = arg;
        }
        then(onResolved, onRejected) {
          console.log("then");
          onResolved && onResolved(this.PromiseResult);
          // then的链式操作 实现 1.返还this 2.返还实例化对象；
          return this;
          // return new MyPromise(res=>{res()});
        }
      }
      let p = new myPromise((res, rej) => {
        // 外部执行
        // res---->this._reslove
        res("success");
        // rej("error");
        // rej--->this._reject
      });
      // console.log(p);
      p.then(
        (res) => {
          console.log(res);
        },
        (err) => {}
      )
        .then()
        .then()
        .then();
    </script>
  </body>
</html>
